package com.javasoft.util.LCB;

import com.javasoft.entity.LCB.UserInfoEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CsvImportUtil {

    private static final Logger logger = LoggerFactory.getLogger(CsvImportUtil.class);

    public static List<UserInfoEntity> parseCsv(InputStreamReader inputStreamReader) throws IOException {
        List<UserInfoEntity> users = new ArrayList<>();
        BufferedReader br = new BufferedReader(inputStreamReader);
        String line;
        boolean isFirstLine = true; // Skip header line

        while ((line = br.readLine()) != null) {
            if (isFirstLine) {
                isFirstLine = false;
                continue;
            }
            String[] values = line.split(",");
            if (values.length >= 5) { // Ensure we have enough columns
                UserInfoEntity user = new UserInfoEntity();
                try {
                    user.setId((long) Integer.parseInt(values[0].trim()));
                } catch (NumberFormatException e) {
                    logger.warn("Invalid ID format for line: {}", line);
                    continue;
                }
                user.setName(values[1].trim());
                user.setPassword(values[2].trim());
                user.setEmail(values[3].trim());
                //user.setPermission(values[4].trim());

                users.add(user);
            } else {
                logger.warn("Insufficient columns in line: {}", line);
            }
        }

        return users;
    }
}



